﻿$(document).ready(function() {
	$("#number").focus(function() {
	}).blur(function() {
		checkNumber();	
	});
	$("#name").focus(function() {
	}).blur(function() {
		checkName();
	});
	$("#password").focus(function() {
	}).blur(function() {
		checkPass();
	});
	$("#password2").focus(function() {
	}).blur(function() {
		checkPass();
	});
	$("#phone").focus(function() {
	}).blur(function() {
		checkMobile($("#phone").val());
	});
	$("#college").change(function(){
		$.post("AjaxAction!checkRegister.action", {
			type : "getMajor",
			value : $('#college option:selected').text()
		}, function(result) {
			$('#major').html(result);
		});
	});
});
function registerCheck(form) {
	if (!checkNumber()) {
		alert("请正确输入学号");
		$('#number').focus();
		return false;
	}
	if (!checkName()) {
		alert("请检查姓名");
		$('#name').focus();
		return false;
	}

	if (!checkPass()) {
		alert("密码格式不正确");
		$('#password').focus();
		return false;
	}

	var phone = document.getElementById("phone");
	if (phone) {
		if (!checkMobile(phone.value)) {
			alert("不正确的手机号码");
			phone.focus();
			return false;
		}
	}
	form.submit();
}

function modifyInfo(form)
{
	if($('#password').val()!='' && $('#password').val()!=$('#password2').val()){
		alert('两次密码不一致');
		$('#password').focus();
		return false;
	}
	var phone = document.getElementById("phone");
	if (phone) {
		if (!checkMobile(phone.value)) {
			alert("不正确的手机号码");
			phone.focus();
			return false;
		}
	}
	form.submit();
}

function checkNumber() {
	if ($('#number').val() == "") {
		$('#hint_number').html('*请输入学号！');
		return false;
	} else {
		$.post("AjaxAction!checkRegister.action", {
			type : "checkNumber",
			number : $('#number').val()
		}, function(result) {
			if(result.trim() == "yes"){
				$("#hint_number").css("color","green");
				$("#hint_number").html("√");
			}
			else{
				$("#hint_number").css("color","red");
				$("#hint_number").html("该学号不可以注册！");
				return false;
			}
		});
		//$('#hint_number').html('*');
	}
	return true;
}

function checkName(){
	if ($('#name').val() == "") {
		$('#hint_name').html('*请输入姓名！');
		return false;
	} else {
		$.post("AjaxAction!checkRegister.action", {
			type : "checkName",
			number : $('#number').val(),
			name : $('#name').val()
		}, function(result) {
			if(result.trim() == "yes"){
				$("#hint_name").css("color","green");
				$("#hint_name").html("√");
			}
			else{
				$("#hint_name").css("color","red");
				$("#hint_name").html("姓名检测错误！");
				return false;
			}
		});
	}
	return true;
}

function checkPass() {
	if ($('#password').val() == "") {
		$('#hint_password').html('*请输入密码！');
		return false;
	} else {
		$('#hint_password').html('*');
	}
	if ($('#password2').val() != $('#password').val()) {
		$('#hint_password2').html('*两次密码不一致！');
		return false;
	} else {
		$('#hint_password2').html('*');
	}
	return true;
}

function checkMobile(phone) {
	if (!(/^1[3|5|8]\d{9}$/.test(phone))) {
		$('#hint_phone').css("color","red");
		$('#hint_phone').html('*不正确的手机号码！');
		return false;
	} else {
		$('#hint_phone').css("color","green");
		$('#hint_phone').html('√');
	}
	return true;
}

function loadStudentInfo(){
	$.post("AjaxAction!getStudentInfo.action", null, function(data) {
		if(data[0].result.trim()=="logout"){
			alert('未登录！请先登录！');
			window.location.href="login.html";
			return;
		}
		$('#name').val(data[0].name);
		$('#nation').val(data[0].nation);
		$('#sex').val(data[0].sex);
		$('#college').val(data[0].college);
		$('#major').val(data[0].major);
		$('#phone').val(data[0].phone);
		$('#politic').val(data[0].politic);
	},"json");
}